package com.gdyx.app.web.web;


import com.gdyx.app.web.annotation.Authorisation;
import com.gdyx.app.web.model.MpsRole;
import com.gdyx.app.web.model.MpsUser;
import com.gdyx.app.web.svc.MpsOrgSvc;
import com.gdyx.app.web.svc.MpsRoleOptSvc;
import com.gdyx.app.web.svc.MpsRoleSvc;
import com.gdyx.app.web.util.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/MpsRole")
public class MpsRoleController extends BaseController {

    @Autowired
    MpsRoleSvc mpsRoleSvc;
    @Autowired
    MpsOrgSvc mpsOrgSvc;
    @Autowired
    MpsRoleOptSvc mpsRoleOptSvc;
    @Autowired
    HttpServletRequest request;

    @Authorisation(value = "/MpsRole/goMpsRoleList")
    @GetMapping("/goMpsRoleList")
    public ModelAndView goMpsRoleList(@ModelAttribute("respMsg") String respMsg) {
        MpsUser nowMpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        String org_session = (String) request.getSession().getAttribute("Org_SESSION");
        ModelAndView view = new ModelAndView();
        List<MpsRole> resultRole = mpsRoleSvc.findMpsRoleByOId(org_session);
        view.addObject("respMsg", respMsg);
        view.addObject("MpsRoleList", resultRole);
        view.setViewName("MpsRoleList");
        return view;
    }

    @Authorisation(value = "/MpsRole/Update")
    @GetMapping("/goMpsRoleEdit")
    public ModelAndView goMpsRoleEdit(@ModelAttribute("respMsg") String respMsg, @RequestParam("id") String id) {
        ModelAndView view = new ModelAndView();
        MpsUser nowMpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        String org_session = (String) request.getSession().getAttribute("Org_SESSION");
        MpsRole findMpsRole = mpsRoleSvc.findMpsRoleById(id);
        if (null == findMpsRole) {
            view.addObject("respMsg", " 修改角色不存在");
            view.setViewName("redirect:/MpsUser/goMpsUserList");
            return view;
        } else {
            view.addObject("R_I", findMpsRole);
            view.addObject("respMsg", respMsg);
            view.setViewName("MpsRoleEdit");
            return view;
        }
    }

    @Authorisation(value = "/MpsRole/Save")
    @GetMapping("/goMpsRoleAdd")
    public ModelAndView goMpsRoleAdd(@ModelAttribute("respMsg") String respMsg) {
        ModelAndView view = new ModelAndView();
        view.addObject("respMsg", respMsg);
        view.setViewName("MpsRoleAdd");
        return view;
    }

    @Authorisation(value = "/MpsRole/Save")
    @PostMapping("/Save")
    public String save(RedirectAttributes attr, MpsRole role) {
        if (StringUtils.isEmpty(role.getName())) {
            attr.addFlashAttribute("respMsg", "操作失败，角色名称不能为空");
            return "redirect:/MpsRole/goMpsRoleAdd";
        }
        Map<String, Object> map = new HashMap<>();
        List<Condition> conditions = new ArrayList<>();
        conditions.add(ruleBuild("and", "name", "=", role.getName(), "S"));
        map.put("conditions", conditions);
        int count = mpsRoleSvc.count(map);
        if (count > 0) {
            attr.addFlashAttribute("respMsg", "操作失败，角色名称已存在");
            return "redirect:/MpsRole/goMpsRoleAdd";
        }
        MpsUser mpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        String org_session = (String) request.getSession().getAttribute("Org_SESSION");
        role.setCreateOwner(mpsUser.getId());
        role.setOrgId(org_session);
        mpsRoleSvc.save(role);
        attr.addFlashAttribute("respMsg", "增加角色：" + role.getName() + "成功");
        return "redirect:/MpsRole/goMpsRoleList";
    }

    @Authorisation(value = "/MpsRole/goMpsRoleList")
    @GetMapping("/Delete")
    public String delete(RedirectAttributes attr, @RequestParam("id") String id) {
        mpsRoleSvc.delete(id);
        attr.addFlashAttribute("respMsg", "删除角色成功");
        return "redirect:/MpsRole/goMpsRoleList";
    }

    @Authorisation(value = "/MpsRole/Update")
    @PostMapping("/Update")
    public String update(RedirectAttributes attr, MpsRole role) {
        if (StringUtils.isEmpty(role.getName())) {
            attr.addFlashAttribute("respMsg", "操作失败，角色名称不能为空");
            return "redirect:/MpsRole/goMpsRoleEdit?id=" + role.getId();
        }
        if (StringUtils.isEmpty(role.getOrgId())) {
            attr.addFlashAttribute("respMsg", "操作失败，机构不能为空");
            return "redirect:/MpsRole/goMpsRoleAdd";
        }
        Map<String, Object> map = new HashMap<>();
        List<Condition> conditions = new ArrayList<>();
        conditions.add(ruleBuild("and", "id", "<>", role.getId(), "S"));
        conditions.add(ruleBuild("and", "name", "=", role.getName(), "S"));
        map.put("conditions", conditions);
        int count = mpsRoleSvc.count(map);
        if (count > 0) {
            attr.addFlashAttribute("respMsg", "操作失败，角色名称已存在");
            return "redirect:/MpsRole/goMpsRoleEdit?id=" + role.getId();
        }
        MpsUser mpsUser = (MpsUser) request.getSession().getAttribute("ADMIN_SESSION");
        role.setModifyOwner(mpsUser.getId());
        mpsRoleSvc.update(role);
        attr.addFlashAttribute("respMsg", "修改角色" + role.getName() + "成功");
        return "redirect:/MpsRole/goMpsRoleList";
    }


}
